import win32clipboard as wcd
import win32con
import win32api
import time
import translate

# 定义了一些key值
vk_code = {'ctrl': 0x11, 'enter': 0x0D, 'a': 0x41, 'v': 0x56, 'x': 0x58}

# 键盘按下
def key_down(keyname):
    win32api.keybd_event(vk_code[keyname], 0, 0, 0)

# 键盘抬起
def key_up(key_name):
    win32api.keybd_event(vk_code[key_name], 0, win32con.KEYEVENTF_KEYUP, 0)

# 按键组合操作
def simulate_key(firstkey, sencondkey):
    key_down(firstkey)
    key_down(sencondkey)
    key_up(sencondkey)
    key_up(firstkey)

def getClipboardData():
    wcd.OpenClipboard()
    data = wcd.GetClipboardData()
    wcd.CloseClipboard()
    return data

def setClipboardData(data):
    wcd.OpenClipboard()
    wcd.EmptyClipboard()
    wcd.SetClipboardData(win32con.CF_UNICODETEXT, data)
    wcd.CloseClipboard()
    return True

def keyoperation():
    try:
        rawtext = getClipboardData()
        time.sleep(0.5)
        tratext = translate.getTranslateResult(rawtext)
        setClipboardData(tratext)
        time.sleep(0.5)
        simulate_key('ctrl', 'v')
        print("successful\n-----------------")
    except:
        print("未知错误")
'''
if __name__ == '__main__':
    main()
'''